//
//  TPLoginViewController.m
//  Yimei
//
//  Created by xiaowu xiaowu on 2017/10/20.
//  Copyright © 2017年 xiaowu xiaowu. All rights reserved.
//

#import "TPLoginViewController.h"
#import "LoginContairView.h"
#import "TPSinginViewController.h"
#import "TPForgetViewController.h"
#import "ViewController.h"
#import <OpenShare/OpenShare.h>
#import <OpenShare+Weixin.h>
@interface TPLoginViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong)LoginContairView *contairView;
@end

@implementation TPLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录";
    [self setup];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.contairView viewWillAppear];
}
-(void)setup{
    
    self.view.backgroundColor = TPAllBackgroundColor;
    UIScrollView *scrollview = [[UIScrollView alloc]init];
    scrollview.delegate = self;
    scrollview.pagingEnabled = YES;
    scrollview.showsVerticalScrollIndicator = NO;
    scrollview.showsHorizontalScrollIndicator = NO;
    scrollview.contentSize = CGSizeMake(IPHONE_WIDTH , 667);
    [self.view addSubview:scrollview];
    [scrollview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    UIView *mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 667)];
    [scrollview addSubview:mainView];
    
    _contairView = [LoginContairView getLoginContairView:mainView];
    @weakify(self);
    _contairView.style = LoginContairViewStyleTelPsw; //快捷登录
    [_contairView setBlock:^(NSInteger tag, UIButton *button) {
        
        @strongify(self);
        switch (tag) {
            case LoginContairViewStyleTelLogin:
            {
                [self loginRequest:button];
            }
                break;
            case LoginContairViewStyleSinin:
            {
                [self.navigationController pushViewController:[TPSinginViewController new] animated:YES];
            }
                break;
            case LoginContairViewStyleFinePass:
            {
                [self.navigationController pushViewController:[TPForgetViewController new] animated:YES];
            }
                break;
            case LoginContairViewStyleCode:{
                
                [[TPRequestClient sharedClient]getRequestCodeButton:button mobile:self.contairView.viewModel.phone toType:LoginCodeStyleLogin];
            }
                break;
                
            default:
                break;
        }
    }];
    
   
    UIButton *telPswButton = [UIButton buttonWithFrame:CGRectZero title:@"账号密码登录" font:fontSystemOfSize(16) color:[UIColor whiteColor]];
    telPswButton.tag = 222;
    [telPswButton addTouchUpInsideTarget:self action:@selector(actionButton:)];
    [self.view addSubview:telPswButton];
    
    UIButton *telButton = [UIButton buttonWithFrame:CGRectZero title:@"手机号快捷登录" font:fontSystemOfSize(16) color:[UIColor whiteColor]];
    [telButton addTouchUpInsideTarget:self action:@selector(actionButton:)];
    telButton.tag = 333;
    [self.view addSubview:telButton];
    
    [telPswButton tp_addLineBottomWithColor:[UIColor whiteColor]];
    UIImageView *line =  [telButton tp_addLineBottomWithColor:[UIColor whiteColor]];
    line.alpha = 0.0;
    
    [telPswButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(-IPHONE_WIDTH*0.25);
        make.top.mas_equalTo(IOS7_TOP_Y+10.0);
        make.height.mas_equalTo(30.0);
    }];
    [telButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(IPHONE_WIDTH *0.25);
        make.top.mas_equalTo(telPswButton.mas_top);
        make.height.mas_equalTo(telPswButton.mas_height);
    }];
   
    
    //
    UIButton *wxButton = [UIButton buttonWithFrame:CGRectZero imageName:@"TP_login_wx_icon"];
    [wxButton addTouchUpInsideTarget:self action:@selector(wxLogin:)];
    [mainView addSubview:wxButton];
    [wxButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_contairView.mas_bottom).offset(120.0);
        make.size.mas_equalTo(CGSizeMake(30.0, 30.0));
        make.centerX.mas_equalTo(self.view);
    }];
    
    UILabel *thirdPartyLabel = [UILabel initWithFrame:CGRectZero font:fontSystemOfSize(11) color:[UIColor whiteColor] aligment:NSTextAlignmentCenter];
    thirdPartyLabel.text = @"第三方登录";
    [mainView addSubview:thirdPartyLabel];
    [thirdPartyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(wxButton.mas_top).offset(-10.0);
        make.size.mas_equalTo(CGSizeMake(100.0, 30.0));
        make.centerX.mas_equalTo(self.view);
    }];
    
    
}


-(void)actionButton:(UIButton *)sender{
    
    
    [self.view endEditing:YES];
    _contairView.loginStyle = sender.tag == 333 ? LoginContairViewStyleTelLogin : LoginContairViewStyleTelPsw;
    
    UIImageView *line1 = [sender viewWithTag:12930];
    UIButton *button = (UIButton *)[self.view viewWithTag:sender.tag == 333 ? 222:333];
    UIImageView *line2 = [button viewWithTag:12930];
    
    
    [UIView animateWithDuration:0.25 animations:^{
        line1.alpha = 1.0;
        line2.alpha = 0.0f;
        
    } completion:^(BOOL finished) {
        ;
    }];
   
    
    
}

-(void)loginRequest:(UIButton *)sender{
    
    [self.view endEditing:YES];
    NSString *code = self.contairView.viewModel.code;
    NSString *psw = self.contairView.viewModel.psw;

    
    if(self.contairView.viewModel.phone.length == 0){
        [self.view xw_hideTitle:@"请填写手机号码"];
        return;
    }
    
    if(self.contairView.style == LoginContairViewStyleTelPsw){
        
        code = @"";
        if (self.contairView.viewModel.psw.length == 0){
            [self.view xw_hideTitle:@"请填写密码"];
            return;
        }
        
    }else{
        
        
        psw = @"";
       if (self.contairView.viewModel.code.length == 0){
            [self.view xw_hideTitle:@"请填写验证码"];
            return;
            
        }
        
    }
    
    
    [sender startSubmitting:@"登录中..."];
    [[TPRequestClient sharedClient]getLoginName:self.contairView.viewModel.phone toPass:psw code:code success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
        
        [sender stopSubmitting];
        [self.view xw_hideTitle:@"登录成功!"];
        [LoginModel LoginSuccess:respone.data type:OtherLoginTypeDefaultPsw];
        [LoginModel shareLoginModel].pass = self.contairView.viewModel.psw;
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self leftBtnAction:nil];
        });
        
        [[NSNotificationCenter defaultCenter]postNotificationName:kNotificationCenterLoginSccuess object:nil];
    } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
        [sender stopSubmitting];
        [self.view xw_hideTitle:respone.message];
    } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
        [self.view xw_hideTitle:error.localizedDescription];
        [sender stopSubmitting];
    }];
    
}



-(void)wxLogin:(UIButton *)sender{
    
  
    [OpenShare WeixinAuth:@"snsapi_userinfo,snsapi_base" Success:^(NSDictionary *message) {
        DLog(@"微信登录\n%@",message);
        
         [self.view xw_showHUDWithTitle:@"授权中..."];
        [[TPRequestClient sharedClient]getWXLoginCode:message[@"code"] success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {

            
            [self.view xw_hideTitle:@"登录成功!"];
            [LoginModel LoginSuccess:respone.data type:OtherLoginTypeWechat];
            
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self leftBtnAction:nil];
            });
            
            [[NSNotificationCenter defaultCenter]postNotificationName:kNotificationCenterLoginSccuess object:nil];
           

        } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {

            [sender stopSubmitting];
            [self.view xw_hideTitle:respone.message];

        } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
            [sender stopSubmitting];
        }];
        
    } Fail:^(NSDictionary *message, NSError *error) {
        DLog(@"微信登录失败\n%@\n%@",error,message);
        [self.view xw_hideTitle:@"授权登录失败"];
    }];
    
}

//
//static CGFloat historyX= 0.0;
//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
//{
//    historyX = scrollView.contentOffset.x;
//}
//
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    if (scrollView.dragging) {//拖拽
//        if (scrollView.contentOffset.x > 0) {//当前月
//            scrollView.scrollEnabled = NO;
//        }
//    }
//
//
//}
//
//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
//{
//    scrollView.scrollEnabled = YES;
//}
//
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
//{
//    scrollView.scrollEnabled = YES;
//}

@end
